package Lee_code;

/**
 * @author blh
 * @date 2024年06月28日 20:38
 */
public class Lee_43_628 {
    public String multiply(String num1, String num2) {
        int len1 = num1.length();
        int len2 = num2.length();
        if ("0".equals(num1) || "0".equals(num2)) {
            return "0";
        }
        String sum = "0";
        // String1为不动产 将String2拆分
        for (int i = 0; i < len1; i++) {
            int tempSum1 = Character.getNumericValue(num1.charAt(len1 - 1 - i));
            for (int j = 0; j < len2; j++) {
                int tempSum2 = Character.getNumericValue(num2.charAt(len2 - 1 - j));
                StringBuilder sb = new StringBuilder(String.valueOf(tempSum1 * tempSum2));
                for (int k = 0; k < i + j; k++) {
                    sb.append('0');
                }
                sum = add(sum, sb.toString());
            }
        }
        return sum;
    }

    /*

      1 2 3
  + 4 6 8 9

    * */
    public String add(String num1, String num2) {
        StringBuilder sum = new StringBuilder();
        int commonLen = Math.min(num1.length(), num2.length());

        // 进位标记位
        int sign = 0;
        for (int i = 0; i < commonLen; i++) {
            int tempNum1 = Character.getNumericValue(num1.charAt(num1.length() - 1 - i));
            int tempNum2 = Character.getNumericValue(num2.charAt(num2.length() - 1 - i));
            int cur = tempNum1 + tempNum2 + sign;
            if (cur >= 10) {
                cur = cur - 10;
                sign = 1;
            } else {
                sign = 0;
            }
            sum.insert(0, cur);
        }
        String largerStr;
        int larger;
        if (num1.length() == num2.length()) {
            if (sign == 1) {
                sum.insert(0, '1');
            }
            return sum.toString();
        } else if (num1.length() > num2.length()) {
            largerStr = num1;
            larger = num1.length() - num2.length();
        } else {
            largerStr = num2;
            larger = num2.length() - num1.length();
        }
        for (int i = 0; i < larger; i++) {
            int tempNum = Character.getNumericValue(largerStr.charAt(larger - 1 - i));
            int cur = tempNum + sign;
            if (cur >= 10) {
                cur = cur - 10;
                sign = 1;
            } else {
                sign = 0;
            }
            sum.insert(0, cur);
        }
        if (sign == 1) {
            sum.insert(0, '1');
        }
        return sum.toString();
    }


}
